package ru.paswd.demo.android.view.ui.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import ru.paswd.demo.android.view.R
import ru.paswd.demo.android.view.models.DecisionItem

class HistoryItemAdapter(
    private val items: List<DecisionItem>,
) : RecyclerView.Adapter<HistoryItemAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val argA = view.findViewById<TextView>(R.id.historyItemArgA)
        val argB = view.findViewById<TextView>(R.id.historyItemArgB)
        val argC = view.findViewById<TextView>(R.id.historyItemArgC)
        val result = view.findViewById<TextView>(R.id.historyItemResult)
        val hr = view.findViewById<View>(R.id.historyItemHr)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_history, parent, false)

        return ViewHolder(view)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        val result = item.result.joinToString(", ")

        holder.argA.text = "A = ${item.argA}"
        holder.argB.text = "B = ${item.argB}"
        holder.argC.text = "C = ${item.argC}"
        holder.result.text = "[ ${result} ]"

        holder.hr.visibility = if (position < items.size - 1) {
            VISIBLE
        } else {
            GONE
        }
    }
}